package com.yao.mssm.system.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yao.mssm.config.DynamicDataSourceHolder;
import com.yao.mssm.config.properties.DBType;
import com.yao.mssm.system.crossDatabase.ICDUserService;
import com.yao.mssm.system.dao.SysUserDao;
import com.yao.mssm.system.entity.Employee;
import com.yao.mssm.system.entity.User;
import com.yao.mssm.system.service.IEmpService;
import com.yao.mssm.system.service.ISysUserService;
import com.yao.mssm.system.service.support.BaseServiceImpl;

@Controller
@RequestMapping("/EmpControl")
public class EmpControl {
	
	private final static Logger logger = LoggerFactory.getLogger(EmpControl.class);
	
	@Autowired
	IEmpService empService;
	
	@Autowired
	ISysUserService sysUserService;
	
	@Autowired
	ICDUserService cDUserService;
	
	@RequestMapping("/getEmpList")
	public String getEmpList(Model model){
		
		List<Employee> list = empService.selectTop5();//empService.selectList(new EntityWrapper<Employee>());
		logger.info("人员查询成功了*************************");
		logger.error("出错了哦！！！！！！！！！！！！！！！！！！！！！");
		
		//list = empService.selectTop5();
		
		model.addAttribute("type", "emp");
		model.addAttribute("list", list);
		
		return "empList";
	}
	
	@ResponseBody
	@RequestMapping("/getEmpById")
	public Object getEmpById(int id){
		long start = System.currentTimeMillis();
		JSONObject obj = new JSONObject();
		Employee emp = empService.selectById(id);
		long end = System.currentTimeMillis();
		logger.info("查询成功++++++++"+emp.getName()+"     :"+(long)(end-start));
		obj.put("result", "success");
		obj.put("data", emp);
		
		return obj;
	}
	
	@RequestMapping("/getUserList")
	public String getUserList(Model model){
		
		List<User> list = sysUserService.selectList(new EntityWrapper<User>());//sysUserService.getList();// 
		logger.info("人员查询成功了*************************");
		logger.error("出错了哦！！！！！！！！！！！！！！！！！！！！！");
		
		model.addAttribute("type", "user");
		model.addAttribute("list", list);
		
		return "empList";
	}
	
	@ResponseBody
	@RequestMapping("/getUserById")
	public Object getUserById(String id){
		JSONObject obj = new JSONObject();
		User user = sysUserService.selectById(id);
		logger.info("查询成功++++++++"+user.getUserName());
		obj.put("result", "success");
		obj.put("data", user);
		return obj;
	}
	
	@RequestMapping("/insetTwoUser")
	public String insetTwoUser(){
		cDUserService.saveUser();
		return "empList";
	}
	
	

}
